package com.dang.alex.dao.Imp;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.dang.alex.dao.ReceiveAddressDAO;
import com.dang.alex.model.ReceiveAddress;
import com.dang.alex.util.DBConnection;
import com.mysql.jdbc.Connection;

public class ReceiveAddressDAOImp implements ReceiveAddressDAO {
	
	private static final String FINDBYID = "select * from d_receive_address where id = ?";
	private static final String FINDBYUSERID = "select d_receive_address.* from d_receive_address where user_id = ?";
	private static final String INSERT = "insert into d_receive_address(user_id,receive_name,full_address,postal_code,mobile,phone) values(?,?,?,?,?,?)";
	
	public ReceiveAddress findById(int id) throws Exception {
		PreparedStatement prep = null;
		Connection conn = null;
		ReceiveAddress addr = null;
		try {
			conn = (Connection) DBConnection.getConnection();
			prep = conn.prepareStatement(FINDBYID);
			prep.setInt(1, id);
			ResultSet rst = prep.executeQuery();
			if (rst.next()) {
				addr = new ReceiveAddress();
				addr.setUserId(rst.getInt("user_id"));
				addr.setReceiveName(rst.getString("receive_name"));
				addr.setFullAddress(rst.getString("full_address"));
				addr.setPostalCode(rst.getString("postal_code"));
				addr.setMobile(rst.getString("mobile"));
				addr.setPhone(rst.getString("phone"));
			}
		}
		finally {
			DBConnection.close(prep, conn);
		}
		return addr;
	}
	
	public void sava(ReceiveAddress addr) throws Exception {
		PreparedStatement prep = null;
		Connection conn = null;
		try {
			conn = (Connection) DBConnection.getConnection();
			prep = conn.prepareStatement(INSERT, Statement.RETURN_GENERATED_KEYS);
			prep.setInt(1, addr.getUserId());
			prep.setString(2, addr.getReceiveName());
			prep.setString(3, addr.getFullAddress());
			prep.setString(4, addr.getPostalCode());
			prep.setString(5, addr.getMobile());
			prep.setString(6, addr.getPhone());
			prep.executeUpdate();
			ResultSet rst = prep.getGeneratedKeys();
			rst.next();
			int id = rst.getInt(1);
			addr.setId(id);
		}
		finally {
			DBConnection.close(prep, conn);
		}
	}
	
	public List<ReceiveAddress> findByUserId(int userId) throws Exception {
		Connection conn = null;
		PreparedStatement prep = null;
		ResultSet rs = null;
		List<ReceiveAddress> receiveAddress = new ArrayList<ReceiveAddress>();
		try {
			conn = (Connection) DBConnection.getConnection();
			prep = conn.prepareStatement(FINDBYUSERID, Statement.RETURN_GENERATED_KEYS);
			prep.setInt(1, userId);
			rs = prep.executeQuery();
			while(rs.next()) {
				ReceiveAddress rd = new ReceiveAddress();
				rd = new ReceiveAddress();
				rd.setFullAddress(rs.getString("full_address"));
				rd.setId(rs.getInt("id"));
				rd.setMobile(rs.getString("mobile"));
				rd.setPhone(rs.getString("phone"));
				rd.setPostalCode(rs.getString("postal_code"));
				rd.setReceiveName(rs.getString("receive_name"));
				rd.setUserId(rs.getInt("user_id"));
				
				receiveAddress.add(rd);
			}
		}
		catch (SQLException e) {
			e.printStackTrace();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			DBConnection.close(prep, conn);
		}
		return receiveAddress;
	}
	
}
